package com.java.service.impl;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import com.java.model.Student;
import com.java.service.StudentService;
import com.java.vo.Result;

/**
 * 使用Set实现学员存储
 */
public class StudentServiceSetImpl implements StudentService {
	
	public static Set<Student> stuList = new HashSet<Student>();

	/**
	 * 新增一个学生
	 */
	public Result add(int id, String name, int age, double score) {
		Student student = new Student();
		student.setId(id);
		student.setAge(age);
		student.setName(name);
		student.setScore(score);
		return add(student);
	}
	
	/**
	 * 新增学生
	 */
	public Result add(Student student) {
		Result result = new Result();
		stuList.add(student);
		return result.success("恭喜您，新增学员成功");
	}
	
	/**
	 * 修改指定学号学生的姓名
	 */
	public Result edit(int id, String name) {
		Student student = new Student();
		student.setId(id);
		student.setName(name);
		return edit(student);
	}
	
	/**
	 * 修改指定学号学生的成绩
	 */
	public Result edit(int id, double score) {
		Student student = new Student();
		student.setId(id);
		student.setScore(score);
		return edit(student);
	}
	
	/**
	 * 修改指定学号学生的所有信息
	 */
	public Result edit(Student student) {
		Result result = new Result();
		Student stu = find(student.getId());
		if (stu == null) {
			return result.fail("该学生不存在，不能进行编辑");
		} else {
			if (student.getAge() > 0) {
				stu.setAge(student.getAge());
			}
			if (student.getName() != null && !"".equals(student.getName())) {
				stu.setName(student.getName());
			}
			if (student.getScore() > 0) {
				stu.setScore(student.getScore());
			}
		}
		return result.success("恭喜你，编辑学员成功");
	}
	
	/**
	 * 删除指定ID的学生
	 */
	public Result delete(int id) {
		Result result = new Result();
		Student stu = find(id);
		if (stu != null) {
			stuList.remove(stu);
			return result.success("恭喜您，删除学员成功");
		} else {
			return result.fail("学员不存在");
		}
	}
	
	/**
	 * 查找指定ID的学生
	 * 
	 * @return Student | null
	 */
	public Student find(int id) {
		if (stuList.isEmpty()) return null;
		for (Student student : stuList) {
			if (id == student.getId()) {
				return student;
			}
		}
		return null;
	}
	
	/**
	 * 打印所有学生信息
	 */
	public Collection<Student> listAll() {
		return stuList;
	}
	
	@Override
	public void save() {
		System.out.println("功能尚未实现");
	}
	
	@Override
	public void load() {
	}
	
}
